/*     */ package dm.jdbc.util;
/*     */ 
/*     */ import dm.jdbc.dataConvertion.Convertion;
/*     */ import dm.jdbc.dbaccess.DBError;
/*     */ import dm.jdbc.driver.DmdbNumeric;
/*     */ import dm.jdbc.innerData.DmdbExecOpt;
/*     */ import dm.jdbc.innerData.DmdbLexWord;
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.sql.SQLException;
/*     */ import java.util.ArrayList;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ExecOptProcessor
/*     */ {
/*  17 */   private ArrayList m_wordList = null;
/*     */   
/*  19 */   private String m_serverEncoding = null;
/*     */   
/*  21 */   private boolean m_escProcessErr = false;
/*     */   
/*     */   public ExecOptProcessor(String serverEncoding, boolean escProcessErr, ArrayList arrList)
/*     */   {
/*  25 */     this.m_wordList = arrList;
/*     */     
/*  27 */     this.m_serverEncoding = serverEncoding;
/*  28 */     this.m_escProcessErr = escProcessErr;
/*     */   }
/*     */   
/*     */   public DmdbExecOpt getExecOpt(String sql) throws SQLException
/*     */   {
/*  33 */     boolean bExistErr = false;
/*  34 */     boolean order_flag = false;
/*  35 */     boolean order_by_flag = false;
/*  36 */     DmdbExecOpt retExecOpt = new DmdbExecOpt();
/*  37 */     retExecOpt.setOrgSql(sql);
/*     */     
/*  39 */     if (this.m_escProcessErr)
/*     */     {
/*  41 */       retExecOpt.setHasErrorOrProcessed(true);
/*  42 */       return retExecOpt;
/*     */     }
/*     */     
/*  45 */     for (int index = 0; index < this.m_wordList.size(); index++)
/*     */     {
/*  47 */       DmdbLexWord lexWordTemp = (DmdbLexWord)this.m_wordList.get(index);
/*  48 */       if (index == 0)
/*     */       {
/*  50 */         if (!checkOptType(lexWordTemp.getM_value()))
/*     */         {
/*  52 */           bExistErr = true;
/*  53 */           break;
/*     */         }
/*     */         
/*  56 */         retExecOpt.addSqlWord(lexWordTemp.getM_value());
/*     */ 
/*     */ 
/*     */ 
/*     */       }
/*  61 */       else if (checkOrderBy_order(lexWordTemp.getM_value()))
/*     */       {
/*  63 */         order_flag = true;
/*  64 */         retExecOpt.addSqlWord(lexWordTemp.getM_value());
/*     */       }
/*     */       else
/*     */       {
/*  68 */         if ((order_flag) && (!order_by_flag))
/*     */         {
/*  70 */           while (lexWordTemp.getM_value().equalsIgnoreCase(" "))
/*     */           {
/*  72 */             retExecOpt.addSqlWord(lexWordTemp.getM_value());
/*     */             
/*  74 */             index++;
/*     */             
/*  76 */             lexWordTemp = (DmdbLexWord)this.m_wordList.get(index);
/*     */           }
/*     */           
/*  79 */           if (checkOrderBy_by(lexWordTemp.getM_value()))
/*     */           {
/*  81 */             order_by_flag = true;
/*  82 */             retExecOpt.addSqlWord(lexWordTemp.getM_value());
/*  83 */             continue;
/*     */           }
/*     */         }
/*     */         
/*  87 */         if (order_by_flag) {
/*  88 */           lexWordTemp.setM_type(1);
/*     */         }
/*  90 */         if ((1 == lexWordTemp.getM_type()) || 
/*  91 */           (3 == lexWordTemp.getM_type()))
/*     */         {
/*  93 */           String str = lexWordTemp.getM_value();
/*     */           
/*     */ 
/*  96 */           if (str.equalsIgnoreCase("function"))
/*     */           {
/*  98 */             DmdbLexWord prevWord = null;
/*  99 */             for (int prevIndex = index - 1; prevIndex >= 0; prevIndex--)
/*     */             {
/* 101 */               prevWord = (DmdbLexWord)this.m_wordList.get(prevIndex);
/* 102 */               if (prevWord.getM_type() != 7) {
/*     */                 break;
/*     */               }
/*     */             }
/*     */             
/* 107 */             if ((prevWord != null) && (prevWord.getM_value().equalsIgnoreCase("with")))
/*     */             {
/* 109 */               bExistErr = true;
/* 110 */               break;
/*     */             }
/*     */           }
/*     */           
/* 114 */           if (str.equalsIgnoreCase("?")) {
/* 115 */             retExecOpt.setToPrepare(true);
/*     */           }
/* 117 */           retExecOpt.addSqlWord(str);
/*     */         }
/*     */         else
/*     */         {
/*     */           try
/*     */           {
/* 123 */             if (lexWordTemp.getM_type() != 7)
/*     */             {
/* 125 */               DmdbLexWord currentWord = null;
/* 126 */               DmdbLexWord nextWord = null;
/* 127 */               DmdbLexWord thirdWord = null;
/* 128 */               int curIndex = index;
/* 129 */               int nextIndex = 0;
/* 130 */               int thirdIndex = 0;
/* 131 */               if (lexWordTemp.getM_value().equals("'"))
/*     */               {
/* 133 */                 curIndex++;
/* 134 */                 currentWord = (DmdbLexWord)this.m_wordList.get(curIndex);
/* 135 */                 nextIndex = curIndex + 2;
/*     */               }
/*     */               else
/*     */               {
/* 139 */                 currentWord = lexWordTemp; }
/* 140 */               for (nextIndex = curIndex + 1; 
/*     */                   
/*     */ 
/* 143 */                   this.m_wordList.size() > nextIndex; nextIndex++)
/*     */               {
/* 145 */                 nextWord = (DmdbLexWord)this.m_wordList.get(nextIndex);
/* 146 */                 if (nextWord.getM_type() != 7)
/*     */                 {
/*     */ 
/*     */ 
/* 150 */                   if (!nextWord.getM_value().equals("'"))
/*     */                     break;
/* 152 */                   nextIndex++;
/* 153 */                   nextWord = (DmdbLexWord)this.m_wordList.get(nextIndex);
/* 154 */                   nextIndex++;
/*     */                   
/*     */ 
/* 157 */                   break;
/*     */                 } }
/* 159 */               for (thirdIndex = nextIndex + 1; this.m_wordList.size() > thirdIndex; thirdIndex++)
/*     */               {
/* 161 */                 thirdWord = (DmdbLexWord)this.m_wordList.get(thirdIndex);
/* 162 */                 if (thirdWord.getM_type() != 7)
/*     */                 {
/*     */ 
/*     */ 
/* 166 */                   if (!thirdWord.getM_value().equals("'"))
/*     */                     break;
/* 168 */                   thirdIndex++;
/* 169 */                   thirdWord = (DmdbLexWord)this.m_wordList.get(thirdIndex);
/* 170 */                   thirdIndex++;
/*     */                   
/* 172 */                   break;
/*     */                 }
/*     */               }
/*     */               
/* 176 */               if ((word_is_const(currentWord)) && (nextWord != null) && (thirdWord != null) && 
/* 177 */                 (word_is_op(nextWord)) && (word_is_const(thirdWord)))
/*     */               {
/* 179 */                 for (int i = index; i <= thirdIndex; i++)
/*     */                 {
/* 181 */                   retExecOpt.addSqlWord(((DmdbLexWord)this.m_wordList.get(i)).getM_value());
/*     */                 }
/* 183 */                 index = thirdIndex;
/*     */ 
/*     */               }
/*     */               else
/*     */               {
/*     */ 
/* 189 */                 index = replaceConstVal(lexWordTemp, retExecOpt, index);
/*     */               }
/*     */               
/*     */             }
/*     */             else
/*     */             {
/* 195 */               index = replaceConstVal(lexWordTemp, retExecOpt, index);
/*     */             }
/*     */             
/*     */           }
/*     */           catch (UnsupportedEncodingException ex)
/*     */           {
/* 201 */             DBError.throwSQLException(ex.toString(), -1);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/* 206 */     if (bExistErr)
/*     */     {
/* 208 */       retExecOpt.setHasErrorOrProcessed(true);
/* 209 */       return retExecOpt;
/*     */     }
/*     */     
/* 212 */     return retExecOpt;
/*     */   }
/*     */   
/*     */   private int replaceConstVal(DmdbLexWord lexWordTemp, DmdbExecOpt retExecOpt, int index)
/*     */     throws UnsupportedEncodingException, SQLException
/*     */   {
/* 218 */     String strValue = lexWordTemp.getM_value();
/*     */     
/* 220 */     switch (lexWordTemp.getM_type())
/*     */     {
/*     */     case 2: 
/* 223 */       long lint = Long.parseLong(strValue, 10);
/* 224 */       if ((lint > 2147483647L) || (lint < -2147483648L))
/*     */       {
/* 226 */         long lval = Long.parseLong(strValue, 10);
/* 227 */         retExecOpt.addParam(Convertion.longToByteArray(lval), 8, 8);
/*     */       }
/*     */       else
/*     */       {
/* 231 */         int vint = Integer.parseInt(strValue, 10);
/* 232 */         retExecOpt.addParam(Convertion.IntToByteArray(vint), 7, 4);
/*     */       }
/*     */       
/* 235 */       break;
/*     */     
/*     */     case 3: 
/* 238 */       if ((strValue.length() > 2) && ((strValue.startsWith("0x")) || (strValue.startsWith("0X"))))
/* 239 */         strValue = strValue.substring(2);
/* 240 */       long lval = Long.parseLong(strValue, 16);
/* 241 */       retExecOpt.addParam(Convertion.longToByteArray(lval), 8, 8);
/* 242 */       break;
/*     */     
/*     */     case 4: 
/* 245 */       double vdbl = Double.parseDouble(strValue);
/* 246 */       retExecOpt.addParam(Convertion.doubleToByteArray(vdbl), 11, 8);
/* 247 */       break;
/*     */     
/*     */     case 5: 
/* 250 */       DmdbNumeric num = new DmdbNumeric();
/* 251 */       retExecOpt.addParam(num.StrToDec(strValue, 0, 0), 9, 0);
/* 252 */       break;
/*     */     
/*     */     case 6: 
/* 255 */       boolean isEmpty = false;
/*     */       
/* 257 */       String str1 = lexWordTemp.getM_value();
/*     */       
/* 259 */       lexWordTemp = (DmdbLexWord)this.m_wordList.get(++index);
/* 260 */       String str2 = lexWordTemp.getM_value();
/*     */       
/* 262 */       if ((str1.equalsIgnoreCase("'")) && (str2.equalsIgnoreCase("'"))) {
/* 263 */         isEmpty = true;
/*     */       }
/* 265 */       int ind = -1;
/* 266 */       if ((ind = str2.indexOf("''")) != -1)
/*     */       {
/* 268 */         StringBuffer str2Buf = new StringBuffer(str2);
/* 269 */         while (ind != -1)
/*     */         {
/* 271 */           str2Buf.replace(ind, ind + 2, "'");
/* 272 */           ind = str2Buf.indexOf("''", ind + 1);
/*     */         }
/*     */         
/* 275 */         str2 = str2Buf.toString();
/*     */       }
/*     */       
/* 278 */       String str = "";
/* 279 */       if (!isEmpty)
/*     */       {
/* 281 */         str = str2;
/* 282 */         index++;
/*     */       }
/*     */       
/* 285 */       byte[] bytes = null;
/*     */       
/* 287 */       if (this.m_serverEncoding == null) {
/* 288 */         bytes = str.getBytes();
/*     */       } else {
/* 290 */         bytes = str.getBytes(this.m_serverEncoding);
/*     */       }
/* 292 */       retExecOpt.addParam(bytes, 2, bytes.length);
/* 293 */       break;
/*     */     
/*     */     default: 
/* 296 */       retExecOpt.addSqlWord(strValue);
/* 297 */       return index++;
/*     */     }
/*     */     
/* 300 */     retExecOpt.addSqlWord("?");
/*     */     
/* 302 */     return index++;
/*     */   }
/*     */   
/*     */   private boolean checkOrderBy_order(String strOrder)
/*     */   {
/* 307 */     if (strOrder.equalsIgnoreCase("ORDER")) {
/* 308 */       return true;
/*     */     }
/* 310 */     return false;
/*     */   }
/*     */   
/*     */   private boolean checkOrderBy_by(String strBy)
/*     */   {
/* 315 */     if (strBy.equalsIgnoreCase("BY")) {
/* 316 */       return true;
/*     */     }
/* 318 */     return false;
/*     */   }
/*     */   
/*     */   private boolean checkOptType(String strType)
/*     */   {
/* 323 */     if ((strType == null) || (strType.length() == 0)) {
/* 324 */       return false;
/*     */     }
/* 326 */     if ((strType.equalsIgnoreCase("INSERT")) || (strType.equalsIgnoreCase("SELECT")) || 
/* 327 */       (strType.equalsIgnoreCase("UPDATE")) || (strType.equalsIgnoreCase("DELETE")) || 
/* 328 */       (strType.equalsIgnoreCase("EXPLAIN")))
/*     */     {
/* 330 */       return true;
/*     */     }
/*     */     
/* 333 */     return false;
/*     */   }
/*     */   
/*     */   private boolean word_is_op(DmdbLexWord word)
/*     */   {
/* 338 */     if (word.getM_type() == 1)
/*     */     {
/* 340 */       if ((word.getM_value().equals("=")) || (word.getM_value().equals(">")) || 
/* 341 */         (word.getM_value().equals("<")) || (word.getM_value().equals("<>")) || 
/* 342 */         (word.getM_value().equals(">=")) || (word.getM_value().equals("<=")) || 
/* 343 */         (word.getM_value().equals("!=")))
/*     */       {
/* 345 */         return true;
/*     */       }
/*     */     }
/* 348 */     return false;
/*     */   }
/*     */   
/*     */   private boolean word_is_const(DmdbLexWord word)
/*     */   {
/* 353 */     if ((word.getM_type() == 1) || (word.getM_type() == 7) || 
/* 354 */       (word.getM_value().equals("'")))
/*     */     {
/* 356 */       return false;
/*     */     }
/* 358 */     return true;
/*     */   }
/*     */ }


/* Location:              D:\Program Files\DBMS_20150813\jdbc\Dm7JdbcDriver17.jar!\dm\jdbc\util\ExecOptProcessor.class
 * Java compiler version: 2 (46.0)
 * JD-Core Version:       0.7.1
 */